package com.donggua.aichebao.commonui.shop;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.donggua.aichebao.commonui.R;

import java.lang.ref.WeakReference;

/**
 * Created by fzh on 2017/8/7.
 */
//com.donggua.aichebao.commonui.shop.PhotoForUploadLayout
public class PhotoForUploadLayout extends RelativeLayout{
    private TextView tvPhoto;
    private ImageView imgPhoto;
    private TextView  btnPhoto;
    private Drawable drawable;
    private int margin = 15;
    private int textSizeType = TypedValue.COMPLEX_UNIT_PX;
    public PhotoForUploadLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        margin = (int)context.getResources().getDimension(R.dimen.photoMargin);
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CustomPhoto);
        addImage(context,ta);
        addPhotoTextView(context,ta);
        addPhotoButton(context,ta);
        ta.recycle();
    }



    private void addImage(Context context,TypedArray ta){
        drawable = ta.getDrawable(R.styleable.CustomPhoto_customPhotoDrawable);
        imgPhoto = new ImageView(context);
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
        imgPhoto.setImageDrawable(drawable);
        imgPhoto.setLayoutParams(layoutParams);
        this.addView(imgPhoto);
    }

    private void addPhotoButton(Context context,TypedArray ta){
        String text = ta.getString(R.styleable.CustomPhoto_customPhotoBtnText);
        int textSize = (int)ta.getDimension(R.styleable.CustomPhoto_customPhotoBtnTextSize,15);
        int color = ta.getColor(R.styleable.CustomPhoto_customPhotoBtnTextColor,0xffffff);
        btnPhoto = new TextView(context);
        btnPhoto.setText(text);
        btnPhoto.setTextSize(textSizeType,textSize);
        btnPhoto.setTextColor(color);
      //  Drawable bk = ta.getDrawable(R.styleable.CustomPhoto_customPhotoBtnBackground);
        RelativeLayout.LayoutParams layoutParams = getMyLayoutParams();
        layoutParams.bottomMargin = margin;
        layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM,RelativeLayout.TRUE);
        btnPhoto.setLayoutParams(layoutParams);
        this.addView(btnPhoto);
    }

    private void addPhotoTextView(Context context,TypedArray ta){
        String text = ta.getString(R.styleable.CustomPhoto_customPhotoText);
        int textSize = (int)ta.getDimension(R.styleable.CustomPhoto_customPhotoTextSize,15);
        int color = ta.getColor(R.styleable.CustomPhoto_customPhotoTextColor,0x77000000);
        tvPhoto = new TextView(context);
        tvPhoto.setText(text);
        tvPhoto.setTextSize(textSizeType,textSize);
        tvPhoto.setTextColor(color);
        RelativeLayout.LayoutParams layoutParams = getMyLayoutParams();
        layoutParams.topMargin = margin;
        layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP,RelativeLayout.TRUE);
        tvPhoto.setLayoutParams(layoutParams);
        this.addView(tvPhoto);
    }
    private RelativeLayout.LayoutParams getMyLayoutParams(){
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL,RelativeLayout.TRUE);
        return layoutParams;
    }

    public ImageView getPhotoImageView(){
        return imgPhoto;
    }

    public ImageView getWeekPhotoImageView(){
        final WeakReference<ImageView> imageViewWeakReference = new WeakReference<ImageView>(imgPhoto);
        ImageView weekImage = imageViewWeakReference.get();
        return weekImage;
    }
}
